package net.hn.hnms.biz.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.material.domain.StockInfo;
import net.hn.hnms.biz.material.mapper.StockInfoMapper;
import net.hn.hnms.biz.material.request.StockInfoReq;
import net.hn.hnms.biz.material.service.IStockInfoService;
import net.hn.hnms.biz.material.vo.StockInfoVO;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ly
 * 库存信息表服务层实现
 * @date 2022-08-26
 */

@Service
public class StockInfoServiceImpl implements IStockInfoService {

    private StockInfoMapper stockInfoMapper;

    public StockInfoServiceImpl(StockInfoMapper stockInfoMapper) {
        this.stockInfoMapper = stockInfoMapper;
    }

    @Override
    public ResponsePage<StockInfoVO> page(StockInfoReq req, PageQuery pageQuery) {
        Page<StockInfo> page = stockInfoMapper.selectPage(pageQuery.build(), buildQuery(req));

        List<StockInfoVO> list =page.getRecords().stream().map(o ->{
            StockInfoVO vo = new StockInfoVO();
            BeanUtil.copyProperties(o, vo);
            return vo;
        }).collect(Collectors.toList());

        //TODO
        //字典转换
        return new ResponsePage<>(list, (int) page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<StockInfo> 查询条件
     */
    private LambdaQueryWrapper<StockInfo> buildQuery(StockInfoReq req){
        return new LambdaQueryWrapper<StockInfo>()
                .like(StrUtil.isNotBlank(req.getSpecificationModel()), StockInfo::getSpecificationModel, req.getSpecificationModel())
                .like(StrUtil.isNotBlank(req.getStockName()), StockInfo::getStockName, req.getStockName())
                .like(StrUtil.isNotBlank(req.getMaterialName()), StockInfo::getMaterialName, req.getMaterialName())
                .like(StrUtil.isNotBlank(req.getMaterialCode()), StockInfo::getMaterialCode, req.getMaterialCode())
                .like(StrUtil.isNotBlank(req.getGoodsPosition()), StockInfo::getGoodsPosition, req.getGoodsPosition())
                .like(StrUtil.isNotBlank(req.getDepositary()), StockInfo::getDepositary, req.getDepositary())
                .like(StrUtil.isNotBlank(req.getMaterialCode()), StockInfo::getMaterialCode, req.getMaterialCode())
                .eq(StrUtil.isNotBlank(req.getStockCode()), StockInfo::getStockCode, req.getStockCode())
                .eq(StrUtil.isNotBlank(req.getMineCode()), StockInfo::getMineCode, req.getMineCode())
                ;
    }

}
